package org.light.complexverb;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.DragonHideStatement;
import org.light.domain.Field;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.Type;
import org.light.domain.Var;
import org.light.easyui.EasyUIPositions;
import org.light.generator.NamedStatementGenerator;
import org.light.generator.NamedStatementListGenerator;
import org.light.utils.MybatisSqlReflector;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class UpdateUploadDomainField extends DomainFieldVerb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;
	}
	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}
	@Override
	public Method generateServiceImplMethod() throws Exception {
		return null;
	}
	@Override
	public Method generateControllerMethod() throws Exception {
		Method method = new Method();
		method.setStandardName(StringUtil.capFirst(getVerbName()));
		method.addSignature(new Signature(1,"c","*gin.Context"));
		Set<String> imports = new TreeSet<>();
		imports.add("bytes");
		imports.add("io");
		method.setAdditionalImports(imports);
		
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,1,"file, _, err := c.Request.FormFile(\"files[]\")"));
		sList.add(new Statement(2000L,1,"defer file.Close()"));
		sList.add(new Statement(3000L,1,"if err != nil {"));
		sList.add(new Statement(4000L,2,"c.JSON(200, gin.H{"));
		sList.add(new Statement(5000L,3,"\"code\":    400,"));
		sList.add(new Statement(6000L,3,"\"msg\":     \"上传失败!\","));
		sList.add(new Statement(7000L,3,"\"success\": false,"));
		sList.add(new Statement(8000L,2,"})"));
		sList.add(new Statement(9000L,2,"return"));
		sList.add(new Statement(10000L,1,"}"));
		sList.add(new Statement(11000L,0,""));
		sList.add(new Statement(12000L,1,"buf := bytes.NewBuffer(nil)"));
		sList.add(new Statement(13000L,0,""));
		sList.add(new Statement(14000L,1,"if _, err := io.Copy(buf, file); err != nil {"));
		sList.add(new Statement(15000L,2,"c.JSON(200, gin.H{"));
		sList.add(new Statement(16000L,3,"\"code\":    400,"));
		sList.add(new Statement(17000L,3,"\"msg\":     \"上传失败!\","));
		sList.add(new Statement(18000L,3,"\"success\": false,"));
		sList.add(new Statement(19000L,2,"})"));
		sList.add(new Statement(20000L,2,"return"));
		sList.add(new Statement(21000L,1,"}"));
		sList.add(new Statement(22000L,0,""));
		sList.add(new Statement(23000L,1,"if buf.Bytes() != nil {"));
		sList.add(new Statement(24000L,2,"temp"+this.domain.getCapFirstDomainName()+".Set"+this.field.getCapFirstFieldName()+"(buf.Bytes())"));
		sList.add(new Statement(25000L,1,"}"));
		sList.add(new Statement(26000L,0,""));
		sList.add(new Statement(27000L,1,"c.JSON(200, gin.H{"));
		sList.add(new Statement(28000L,2,"\"code\":    200,"));
		sList.add(new Statement(29000L,2,"\"msg\":     \"上传成功!\","));
		sList.add(new Statement(30000L,2,"\"success\": true,"));
		sList.add(new Statement(31000L,2,"\"rows\":    buf.Bytes(),"));
		sList.add(new Statement(32000L,1,"})"));
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}
	
	public UpdateUploadDomainField(Domain domain,Field field){
		super();
		this.domain = domain;
		this.field = field;
		this.verbName = "UpdateUpload"+this.domain.getStandardName()+this.field.getCapFirstFieldName();
		this.setLabel("更新上传");
	}

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		JavascriptBlock block = new JavascriptBlock();
		block.setStandardName("upload"+StringUtil.capFirst(this.domain.getStandardName())+this.field.getCapFirstFieldName());
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"$('#"+this.domain.getLowerFirstDomainName()+this.field.getCapFirstFieldName()+"Fileupload').fileupload({"));
		sList.add(new Statement(2000L,2,"autoUpload: true,"));
		sList.add(new Statement(3000L,2,"dataType: 'json',"));
		sList.add(new Statement(4000L,2,"async: false,"));
		sList.add(new Statement(5000L,2,"formData : function() {"));
		sList.add(new Statement(6000L,3,"return [{name:\""+this.domain.getDomainId().getLowerFirstFieldName()+"\",value:$(\"#ffedit\").find(\"#"+this.domain.getDomainId().getLowerFirstFieldName()+"\").val()}"));
		sList.add(new Statement(7000L,4,"];"));
		sList.add(new Statement(8000L,2,"},"));
		sList.add(new Statement(9000L,2,"success: function(data, textStatus) {"));
		sList.add(new Statement(10000L,2,"if (data.success == true){"));
		sList.add(new Statement(11000L,3,"$(\"#ffedit\").find(\"#"+this.field.getLowerFirstFieldName()+"\").prop(\"src\",\"data:image/png;base64,\"+data.rows)"));
		sList.add(new Statement(12000L,2,"}"));
		sList.add(new Statement(13000L,2,"},"));
		sList.add(new Statement(14000L,1,"progressall: function (e, data) {"));
		sList.add(new Statement(15000L,1,"var progress = parseInt(data.loaded / data.total * 100, 10);"));
		sList.add(new Statement(16000L,1,"},"));
		sList.add(new Statement(17000L,0,"});"));
		block.setMethodStatementList(WriteableUtil.merge(sList));
		return block;

	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		JavascriptMethod method = new JavascriptMethod();
		method.setSerial(200);
		method.setStandardName("show"+this.domain.getCapFirstDomainName()+this.field.getCapFirstFieldName()+"Image");
		
		Signature s1 = new Signature(1,"value","var");
		Signature s2 = new Signature(2,"row","var");
		Signature s3 = new Signature(2,"index","var");
		method.addSignature(s1);
		method.addSignature(s2);
		method.addSignature(s3);
		
		StatementList sList = new StatementList();
		sList.add(new Statement(1000L,1,"if(row."+this.field.getLowerFirstFieldName()+"){"));
		sList.add(new Statement(2000L,2,"return \"<img style='height:50px;' border='1' src='data:image/png;base64,\"+row."+this.field.getLowerFirstFieldName()+"+\"'/>\";"));
		sList.add(new Statement(3000L,1,"} else {"));
		sList.add(new Statement(4000L,2,"return \"<img style='height:50px;' border='1' src='../css/images/blank.jpg'/>\";"));
		sList.add(new Statement(5000L,1,"}"));
		
		method.setMethodStatementList(sList);
		return method;		
	}
}
